#ifndef DOWNLOADSERVICE_H
#define DOWNLOADSERVICE_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkReply>
#include <QFile>
#include <QProgressBar>
#include <QTimer>
#include <QDebug>

#include <queue>

#include "config.h"
#include "downloadinfo.h"

using std::priority_queue;
using std::vector;

class DownloadService : public QObject
{
    Q_OBJECT

    QNetworkAccessManager networkManager;
    priority_queue< const DownloadInfo*, vector< const DownloadInfo*>, DownloadInfo::DownloadInfoComparator> downloadsQueue;
    QNetworkReply *currentNeworkHandler;
    const DownloadInfo *currentDownload;
    QFile currentOutputFile;

public:
    explicit DownloadService(QObject *parent = 0);

    void addDownload(const DownloadInfo* task);
    
signals:
    void progressChanged(const DownloadInfo* download, double progress);
    
public slots:
    void newDownload();
    void finished();
    void readyRead();
    void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
};

#endif // DOWNLOADSERVICE_H
